<?php
class Arg {

    const PROTO = 'proto';

    const PARAM = 'param';

    const PLAYER_ID = 'id';

    const SESSION_ID = 'session';

    const PLATFORM_ID = 'platform_id';

    const DEVICE_ID = 'device_id';

    private $m_arrKeyType = array(
            Arg::PROTO => 'int',
            Arg::PARAM => 'string',
            Arg::PLAYER_ID => 'int',
            Arg::SESSION_ID => 'string',
            Arg::PLATFORM_ID => 'int',
            Arg::DEVICE_ID => 'string' 
    );

    private $m_arg;

    function __construct() {
        $this->_cleanAll();
    }
    
    public function get( $type /* = Arg::PROTO */ ) {
        return $this->_get( $type );
    }

    private function _get( $key ) {
        if ( !isset( $this->m_arg[$key] ) ) {
            Logs::Debug(  'Key null!' . "[$key]" );
            throw new \Exception('Key null!' . "[$key]");
            return null;
        }
        return $this->m_arg[$key];
    }

    private function _cleanAll() {
        $this->m_arg = array();
        
        foreach ( $this->m_arrKeyType as $key => $type ) {
            $this->clean( $key );
        }
        
        Logs::Debug(  'Arg : ' . var_export($this->m_arg, true) );
    }

    private function clean( $key ) {
        if ( !isset( $this->m_arrKeyType[$key] ) ) {
            throw new \Exception('Key null!' . "[$key]");
            return null;
        }
        
        if ( $this->m_arrKeyType[$key] == 'int' ) {
            $this->m_arg[$key] = 0;
        }
        else if ( $this->m_arrKeyType[$key] == 'string' ) {
            $this->m_arg[$key] = null;
        }
        else {
            $this->m_arg[$key] = null;
        }
        
        $this->_set( $key );
        
        if ( $this->m_arrKeyType[$key] == 'int' ) {
            $this->m_arg[$key] = (int)$this->m_arg[$key];
        }
    }

    private function _set( $key ) {
        if ( isset( $_POST[$key] ) ) {
            $this->m_arg[$key] = $_POST[$key];
            return TRUE;
        }
        if ( isset( $GLOBALS["HTTP_RAW_POST_DATA"][$key] ) ) {
            $this->m_arg[$key] = $GLOBALS["HTTP_RAW_POST_DATA"][$key];
            return TRUE;
        }
        if ( isset( $_GET[$key] ) ) {
            $this->m_arg[$key] = $_GET[$key];
            return TRUE;
        }

        // @@@
        //return false;
        throw new \Exception('_set null!' . "[$key]");
    }

}